package com.xinke.module.system.service.mail;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.mail.*;
import com.xinke.module.system.model.entity.mail.MailAccountDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.mail.MailAccountMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 邮箱账号 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class MailAccountServiceImpl implements MailAccountService {

    @Resource
    private MailAccountMapper mailAccountMapper;

    @Override
    public Long createMailAccount(MailAccountSaveReqVO createReqVO) {
        // 插入
        MailAccountDO mailAccount = BeanUtils.toBean(createReqVO, MailAccountDO.class);
        mailAccountMapper.insert(mailAccount);
        // 返回
        return mailAccount.getMailAcctId();
    }

    @Override
    public void updateMailAccount(MailAccountSaveReqVO updateReqVO) {
        // 校验存在
        validateMailAccountExists(updateReqVO.getMailAcctId());
        // 更新
        MailAccountDO updateObj = BeanUtils.toBean(updateReqVO, MailAccountDO.class);
        mailAccountMapper.updateById(updateObj);
    }

    @Override
    public void deleteMailAccount(Long mailAcctId) {
        // 校验存在
        validateMailAccountExists(mailAcctId);
        // 删除
        mailAccountMapper.deleteById(mailAcctId);
    }

    private void validateMailAccountExists(Long mailAcctId) {
        if (mailAccountMapper.selectById(mailAcctId) == null) {
            throw exception(null);
        }
    }

    @Override
    public MailAccountDO getMailAccount(Long mailAcctId) {
        return mailAccountMapper.selectById(mailAcctId);
    }

    @Override
    public PageResult<MailAccountDO> getMailAccountPage(MailAccountPageReqVO pageReqVO) {
        return mailAccountMapper.selectPage(pageReqVO);
    }

}